package com.cnfangmao.masterdata.controller;

import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiImplicitParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.dto.AccountDto;
import com.cnfangmao.masterdata.model.Account;
import com.cnfangmao.masterdata.service.AccountService;


/**
 * <p>
 * 主数据中心用户 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@RequestMapping("/account")
@Api(value = "AccountController 中心用户增删查改相关的api", tags = "Account")
@RestController
public class AccountController {

	@Autowired
	private AccountService accountService;
	
	
	 /**
     * 用户列表
     */
    @RequestMapping(value="/list.do", method = RequestMethod.GET)
    @ApiOperation(value="用户列表", notes = "")
    @ApiImplicitParams({
   	 @ApiImplicitParam(name = "username", value = "用户名称", paramType = "query", dataType = "string"),
   	 @ApiImplicitParam(name = "pageNo", value = "当前页", paramType = "query", dataType = "int"),
   	 @ApiImplicitParam(name = "pageSize", value = "显示条数", paramType = "query", dataType = "int")
   })
    @RequiresPermissions("account:list")
    public ResBody listAccount( String username,
    		@RequestParam(defaultValue="1",required=false) int pageNo,
    		@RequestParam(defaultValue="10",required=false) int pageSize)
    {
    	ResBody resBody = new ResBody();
    	IPage<Account> data =accountService.queryListAccount(username, pageNo, pageSize);
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(data);
        resBody.message("查询成功");
    	return resBody;
    }
    
	   /**
     * 创建用户
     */
    @RequestMapping(value="/create.do", method = RequestMethod.POST)
    @ApiOperation(value="创建用户", notes = "")
    @ApiImplicitParam(name = "input", value = "创建用户信息",required = true, paramType = "body", dataType = "AccountDto")
    @RequiresPermissions("account:add")
    public ResBody createAccount(@Validated @RequestBody  @NotNull AccountDto input){
    	ResBody resBody = new ResBody();
    	if(input.getUserName().isEmpty())
    	{
    		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
        	resBody.message("用户名为空");
            return resBody;
    	}
    	if(accountService.findByUsername(input.getUserName())!=null)
    	{
    		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
        	resBody.message("用户名已经存在");
            return resBody;
    	}
    	accountService.saveAccount(input, "");
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(true);
        resBody.message("创建成功");
    	return resBody;
    }
    
    
    /**
     * 更新用户信息
     */
    @RequestMapping(value="/update.do", method = RequestMethod.POST)
    @ApiOperation(value="更新用户", notes = "")
    @ApiImplicitParam(name = "input", value = "更新用户信息",required = true, paramType = "body", dataType = "AccountDto")
    @RequiresPermissions("account:update")
    public ResBody updateAccount(@Validated @RequestBody  @NotNull AccountDto input){
    	ResBody resBody = new ResBody();
    	if(input.getUserName().isEmpty())
    	{
    		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
        	resBody.message("用户名为空");
            return resBody;
    	}
    	Account account=accountService.findByUsername(input.getUserName());
    	if(account!=null&&!account.getUserId().equals(input.getUserId()))
		{
    		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
        	resBody.message("用户名已经存在");
            return resBody;
		}
    	accountService.update(input, "");
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(true);
        resBody.message("修改成功");
    	return resBody;
    }

    @RequestMapping(value="/delete.do", method = RequestMethod.POST)
    @ApiOperation(value="删除用户", notes = "")
    @ApiImplicitParam(name = "userId", value = "用户userId", required = true, dataType = "String")
    @RequiresPermissions("account:delete")
    public ResBody deleteAccount(@Validated @RequestParam @NotNull String userId){
    	ResBody resBody = new ResBody();
    	accountService.delete(userId);
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(true);
        resBody.message("删除成功");
    	return resBody;
    }
}

